<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.8.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"github.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="仰面朝天，知此身渺渺，沧海一粟">
<meta property="og:type" content="website">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="https://github.com/LBraveHeart/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="仰面朝天，知此身渺渺，沧海一粟">
<meta property="og:locale" content="zh-CN">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Hexo">
<meta name="twitter:description" content="仰面朝天，知此身渺渺，沧海一粟">

<link rel="canonical" href="https://github.com/LBraveHeart/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false
  };
</script>

  <title>Hexo</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hexo</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off" placeholder="搜索..." spellcheck="false" type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2021/03/24/码/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2021/03/24/码/" class="post-title-link" itemprop="url">使用zxing生成二维码</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-03-24 10:53:28 / 修改时间：10:57:19" itemprop="dateCreated datePublished" datetime="2021-03-24T10:53:28+08:00">2021-03-24</time>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>6.2k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="1、引入zxing的依赖"><a href="#1、引入zxing的依赖" class="headerlink" title="1、引入zxing的依赖"></a>1、引入zxing的依赖</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.google.zxing<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>javase<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="2、二维码工具类的实现"><a href="#2、二维码工具类的实现" class="headerlink" title="2、二维码工具类的实现"></a>2、二维码工具类的实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.psk.hms.base.util;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.*;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.client.j2se.BufferedImageLuminanceSource;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.common.BitMatrix;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.common.HybridBinarizer;</span><br><span class="line"><span class="keyword">import</span> com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;</span><br><span class="line"><span class="keyword">import</span> javax.imageio.ImageIO;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.geom.RoundRectangle2D;</span><br><span class="line"><span class="keyword">import</span> java.awt.image.BufferedImage;</span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.OutputStream;</span><br><span class="line"><span class="keyword">import</span> java.util.Hashtable;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 二维码生成及读取工具类</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> chenj.</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 2019/3/14 18:46</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">QRCodeUtil</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String CHARSET = <span class="string">"UTF-8"</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String FORMAT_NAME = <span class="string">"JPG"</span>;</span><br><span class="line">    <span class="comment">// 二维码尺寸</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> QRCODE_SIZE = <span class="number">200</span>;</span><br><span class="line">    <span class="comment">// LOGO宽度</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> WIDTH = <span class="number">60</span>;</span><br><span class="line">    <span class="comment">// LOGO高度</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> HEIGHT = <span class="number">60</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建二维码图片</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content      二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> logoImgPath  Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> needCompress 是否压缩Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回二维码图片</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> WriterException</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> IOException     BufferedImage</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> BufferedImage <span class="title">createImage</span><span class="params">(String content, String logoImgPath, <span class="keyword">boolean</span> needCompress)</span> <span class="keyword">throws</span> WriterException, IOException </span>&#123;</span><br><span class="line">        Hashtable&lt;EncodeHintType, Object&gt; hints = <span class="keyword">new</span> Hashtable&lt;EncodeHintType, Object&gt;();</span><br><span class="line">        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);</span><br><span class="line">        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);</span><br><span class="line">        hints.put(EncodeHintType.MARGIN, <span class="number">1</span>);</span><br><span class="line">        BitMatrix bitMatrix = <span class="keyword">new</span> MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);</span><br><span class="line">        <span class="keyword">int</span> width = bitMatrix.getWidth();</span><br><span class="line">        <span class="keyword">int</span> height = bitMatrix.getHeight();</span><br><span class="line">        BufferedImage image = <span class="keyword">new</span> BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>; x &lt; width; x++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> y = <span class="number">0</span>; y &lt; height; y++) &#123;</span><br><span class="line">                image.setRGB(x, y, bitMatrix.get(x, y) ? <span class="number">0xFF000000</span> : <span class="number">0xFFFFFFFF</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (logoImgPath == <span class="keyword">null</span> || <span class="string">""</span>.equals(logoImgPath)) &#123;</span><br><span class="line">            <span class="keyword">return</span> image;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 插入图片</span></span><br><span class="line">        QRCodeUtil.insertImage(image, logoImgPath, needCompress);</span><br><span class="line">        <span class="keyword">return</span> image;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 添加Logo</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> source       二维码图片</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> logoImgPath  Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> needCompress 是否压缩Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> IOException void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">insertImage</span><span class="params">(BufferedImage source, String logoImgPath, <span class="keyword">boolean</span> needCompress)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        File file = <span class="keyword">new</span> File(logoImgPath);</span><br><span class="line">        <span class="keyword">if</span> (!file.exists()) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        Image src = ImageIO.read(<span class="keyword">new</span> File(logoImgPath));</span><br><span class="line">        <span class="keyword">int</span> width = src.getWidth(<span class="keyword">null</span>);</span><br><span class="line">        <span class="keyword">int</span> height = src.getHeight(<span class="keyword">null</span>);</span><br><span class="line">        <span class="keyword">if</span> (needCompress) &#123; <span class="comment">// 压缩LOGO</span></span><br><span class="line">            <span class="keyword">if</span> (width &gt; WIDTH) &#123;</span><br><span class="line">                width = WIDTH;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (height &gt; HEIGHT) &#123;</span><br><span class="line">                height = HEIGHT;</span><br><span class="line">            &#125;</span><br><span class="line">            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);</span><br><span class="line">            BufferedImage tag = <span class="keyword">new</span> BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);</span><br><span class="line">            Graphics g = tag.getGraphics();</span><br><span class="line">            g.drawImage(image, <span class="number">0</span>, <span class="number">0</span>, <span class="keyword">null</span>); <span class="comment">// 绘制缩小后的图</span></span><br><span class="line">            g.dispose();</span><br><span class="line">            src = image;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 插入LOGO</span></span><br><span class="line">        Graphics2D graph = source.createGraphics();</span><br><span class="line">        <span class="keyword">int</span> x = (QRCODE_SIZE - width) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> y = (QRCODE_SIZE - height) / <span class="number">2</span>;</span><br><span class="line">        graph.drawImage(src, x, y, width, height, <span class="keyword">null</span>);</span><br><span class="line">        Shape shape = <span class="keyword">new</span> RoundRectangle2D.Float(x, y, width, width, <span class="number">6</span>, <span class="number">6</span>);</span><br><span class="line">        graph.setStroke(<span class="keyword">new</span> BasicStroke(<span class="number">3f</span>));</span><br><span class="line">        graph.draw(shape);</span><br><span class="line">        graph.dispose();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成带Logo的二维码</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content      二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> logoImgPath  Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> destPath     二维码输出路径</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> needCompress 是否压缩Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, String logoImgPath, String destPath, <span class="keyword">boolean</span> needCompress)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        BufferedImage image = QRCodeUtil.createImage(content, logoImgPath, needCompress);</span><br><span class="line">        mkdirs(destPath);</span><br><span class="line">        ImageIO.write(image, FORMAT_NAME, <span class="keyword">new</span> File(destPath));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成不带Logo的二维码</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content  二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> destPath 二维码输出路径</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, String destPath)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        QRCodeUtil.encode(content, <span class="keyword">null</span>, destPath, <span class="keyword">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成带Logo的二维码，并输出到指定的输出流</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content      二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> logoImgPath  Logo</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> output       输出流</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> needCompress 是否压缩Logo</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, String logoImgPath, OutputStream output, <span class="keyword">boolean</span> needCompress)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        BufferedImage image = QRCodeUtil.createImage(content, logoImgPath, needCompress);</span><br><span class="line">        ImageIO.write(image, FORMAT_NAME, output);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 生成不带Logo的二维码，并输出到指定的输出流</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> content 二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> output  输出流</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, OutputStream output)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        QRCodeUtil.encode(content, <span class="keyword">null</span>, output, <span class="keyword">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 二维码解析</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> file 二维码</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回解析得到的二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception String</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">decode</span><span class="params">(File file)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        BufferedImage image;</span><br><span class="line">        image = ImageIO.read(file);</span><br><span class="line">        <span class="keyword">if</span> (image == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        BufferedImageLuminanceSource source = <span class="keyword">new</span> BufferedImageLuminanceSource(image);</span><br><span class="line">        BinaryBitmap bitmap = <span class="keyword">new</span> BinaryBitmap(<span class="keyword">new</span> HybridBinarizer(source));</span><br><span class="line">        Result result;</span><br><span class="line">        Hashtable&lt;DecodeHintType, Object&gt; hints = <span class="keyword">new</span> Hashtable&lt;DecodeHintType, Object&gt;();</span><br><span class="line">        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);</span><br><span class="line">        result = <span class="keyword">new</span> MultiFormatReader().decode(bitmap, hints);</span><br><span class="line">        String resultStr = result.getText();</span><br><span class="line">        <span class="keyword">return</span> resultStr;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 二维码解析</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> path 二维码存储位置</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回解析得到的二维码内容</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception String</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">decode</span><span class="params">(String path)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> QRCodeUtil.decode(<span class="keyword">new</span> File(path));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断路径是否存在，如果不存在则创建</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> dir 目录</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">mkdirs</span><span class="params">(String dir)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (dir != <span class="keyword">null</span> &amp;&amp; !<span class="string">""</span>.equals(dir)) &#123;</span><br><span class="line">            File file = <span class="keyword">new</span> File(dir);</span><br><span class="line">            <span class="keyword">if</span> (!file.isDirectory()) &#123;</span><br><span class="line">                file.mkdirs();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="3、测试"><a href="#3、测试" class="headerlink" title="3、测试"></a>3、测试</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">生成不带logo的二维码</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    String dir = <span class="string">"d:/qrcode.jpg"</span>;</span><br><span class="line">    String content = <span class="string">"www.baidu.com"</span>;</span><br><span class="line">    File file = <span class="keyword">new</span> File(dir);</span><br><span class="line">    QRCodeUtil.encode(content, <span class="keyword">new</span> FileOutputStream(file));</span><br><span class="line">    System.out.println(QRCodeUtil.decode(file));</span><br><span class="line">&#125;</span><br><span class="line">生成带logo的二维码</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    String dir = <span class="string">"d:/qrcode.jpg"</span>;</span><br><span class="line">    String content = <span class="string">"www.baidu.com"</span>;</span><br><span class="line">    String logoImgPath = <span class="string">"d:/logo.png"</span>;</span><br><span class="line">    File file = <span class="keyword">new</span> File(dir);</span><br><span class="line">    QRCodeUtil.encode(content, logoImgPath, <span class="keyword">new</span> FileOutputStream(file), <span class="keyword">true</span>);</span><br><span class="line">    System.out.println(QRCodeUtil.decode(file));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/09/10/va阻塞队列/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/09/10/va阻塞队列/" class="post-title-link" itemprop="url">java阻塞队列</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-09-10 22:39:00 / 修改时间：22:45:27" itemprop="dateCreated datePublished" datetime="2020-09-10T22:39:00+08:00">2020-09-10</time>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>136</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>1 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="阻塞队列"><a href="#阻塞队列" class="headerlink" title="阻塞队列"></a>阻塞队列</h2><table>
<thead>
<tr>
<th>方法\处理方式</th>
<th>抛出异常</th>
<th>返回特殊值</th>
<th>一直阻塞</th>
<th>超时退出</th>
</tr>
</thead>
<tbody>
<tr>
<td>插入方法</td>
<td>add(e)</td>
<td>offer(e)</td>
<td>put(e)</td>
<td>offer(e,time,unit)</td>
</tr>
<tr>
<td>移除方法</td>
<td>remove(e)</td>
<td>poll()</td>
<td>take()</td>
<td>poll(time,unit)</td>
</tr>
<tr>
<td>检查方法</td>
<td>element(e)</td>
<td>peek()</td>
<td>不可用</td>
<td>不可用</td>
</tr>
</tbody>
</table>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/04/14/t切换分支/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/04/14/t切换分支/" class="post-title-link" itemprop="url">git切换分支</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-04-14 11:56:30 / 修改时间：12:03:19" itemprop="dateCreated datePublished" datetime="2020-04-14T11:56:30+08:00">2020-04-14</time>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>176</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>1 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="远程拉取项目"><a href="#远程拉取项目" class="headerlink" title="远程拉取项目"></a>远程拉取项目</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone xxxx.git</span><br></pre></td></tr></table></figure>
<h2 id="查看分支"><a href="#查看分支" class="headerlink" title="查看分支"></a>查看分支</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -a</span><br></pre></td></tr></table></figure>
<h2 id="切换分支"><a href="#切换分支" class="headerlink" title="切换分支"></a>切换分支</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b dev origin/dev</span><br></pre></td></tr></table></figure>
<h2 id="从指定分支更新项目"><a href="#从指定分支更新项目" class="headerlink" title="从指定分支更新项目"></a>从指定分支更新项目</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull origin dev</span><br></pre></td></tr></table></figure>
<h2 id="提交分支"><a href="#提交分支" class="headerlink" title="提交分支"></a>提交分支</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin dev</span><br></pre></td></tr></table></figure>
<h2 id="连接分支"><a href="#连接分支" class="headerlink" title="连接分支"></a>连接分支</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push -set upstream</span><br></pre></td></tr></table></figure>
<h2 id="查看当前-远程url"><a href="#查看当前-远程url" class="headerlink" title="查看当前 远程url"></a>查看当前 远程url</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote -v</span><br></pre></td></tr></table></figure>
<h2 id="git-帮助文档"><a href="#git-帮助文档" class="headerlink" title="git 帮助文档"></a>git 帮助文档</h2><p><a href="https://help.github.com/cn/github/using-git/pushing-commits-to-a-remote-repository" target="_blank" rel="noopener">git帮助文档</a></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/03/25/etty的基本使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/25/etty的基本使用/" class="post-title-link" itemprop="url">Netty的基本使用</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-03-25 09:14:39 / 修改时间：09:14:45" itemprop="dateCreated datePublished" datetime="2020-03-25T09:14:39+08:00">2020-03-25</time>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3.4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="Netty的基本使用"><a href="#Netty的基本使用" class="headerlink" title="Netty的基本使用"></a>Netty的基本使用</h1><h2 id="服务端定义"><a href="#服务端定义" class="headerlink" title="服务端定义"></a>服务端定义</h2><h3 id="开启服务端端口"><a href="#开启服务端端口" class="headerlink" title="开启服务端端口"></a>开启服务端端口</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyServer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        EventLoopGroup boseGroup = <span class="keyword">new</span> NioEventLoopGroup();</span><br><span class="line">        EventLoopGroup workerGroup = <span class="keyword">new</span> NioEventLoopGroup();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            ServerBootstrap serverBootstrap = <span class="keyword">new</span> ServerBootstrap();</span><br><span class="line">            <span class="comment">// handler 只针对boseGroup</span></span><br><span class="line">            <span class="comment">// childHandler 经过boseGroup后再经过workerGroup</span></span><br><span class="line">            serverBootstrap.group(boseGroup,workerGroup).channel(NioServerSocketChannel.class)</span><br><span class="line">                    .childHandler(<span class="keyword">new</span> MyServerInitializer());</span><br><span class="line">            ChannelFuture channelFuture = serverBootstrap.bind(<span class="number">8899</span>).sync();</span><br><span class="line">            channelFuture.channel().closeFuture().sync();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            boseGroup.shutdownGracefully();</span><br><span class="line">            workerGroup.shutdownGracefully();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="定义服务端initialize"><a href="#定义服务端initialize" class="headerlink" title="定义服务端initialize"></a>定义服务端initialize</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyServerInitializer</span> <span class="keyword">extends</span> <span class="title">ChannelInitializer</span>&lt;<span class="title">SocketChannel</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">initChannel</span><span class="params">(SocketChannel ch)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        ChannelPipeline pipeline = ch.pipeline();</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,<span class="number">0</span>,<span class="number">4</span>,<span class="number">0</span>,<span class="number">4</span>));</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> LengthFieldPrepender(<span class="number">4</span>));</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> StringDecoder(CharsetUtil.UTF_8));</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> StringEncoder(CharsetUtil.UTF_8));</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> MyHttpServerHandler());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="定义服务端handler"><a href="#定义服务端handler" class="headerlink" title="定义服务端handler"></a>定义服务端handler</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyHttpServerHandler</span> <span class="keyword">extends</span> <span class="title">SimpleChannelInboundHandler</span>&lt;<span class="title">String</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">channelRead0</span><span class="params">(ChannelHandlerContext ctx, String msg)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        System.out.println(ctx.channel().remoteAddress() + <span class="string">", "</span> + msg);</span><br><span class="line">        ctx.channel().writeAndFlush(<span class="string">"from server "</span>+ UUID.randomUUID());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">exceptionCaught</span><span class="params">(ChannelHandlerContext ctx, Throwable cause)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        cause.printStackTrace();</span><br><span class="line">        ctx.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="客户端定义"><a href="#客户端定义" class="headerlink" title="客户端定义"></a>客户端定义</h2><h3 id="绑定客户端端口和ip"><a href="#绑定客户端端口和ip" class="headerlink" title="绑定客户端端口和ip"></a>绑定客户端端口和ip</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyClient</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        EventLoopGroup eventExecutors = <span class="keyword">new</span> NioEventLoopGroup();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Bootstrap bootstrap = <span class="keyword">new</span> Bootstrap();</span><br><span class="line">            bootstrap.group(eventExecutors).channel(NioSocketChannel.class)</span><br><span class="line">                .handler(<span class="keyword">new</span> MyClientInitializer());</span><br><span class="line">            ChannelFuture localhost = bootstrap.connect(<span class="string">"localhost"</span>, <span class="number">8899</span>).sync();</span><br><span class="line">            localhost.channel().closeFuture().sync();</span><br><span class="line"></span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">            eventExecutors.shutdownGracefully();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="定义客户端initialize"><a href="#定义客户端initialize" class="headerlink" title="定义客户端initialize"></a>定义客户端initialize</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyClientInitializer</span> <span class="keyword">extends</span> <span class="title">ChannelInitializer</span>&lt;<span class="title">SocketChannel</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">initChannel</span><span class="params">(SocketChannel ch)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        ChannelPipeline pipeline = ch.pipeline();</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,<span class="number">0</span>,<span class="number">4</span>,<span class="number">0</span>,<span class="number">4</span>));</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> LengthFieldPrepender(<span class="number">4</span>));</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> StringDecoder(CharsetUtil.UTF_8));</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> StringEncoder(CharsetUtil.UTF_8));</span><br><span class="line">        pipeline.addLast(<span class="keyword">new</span> MyClientHandler());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="定义客户端handler"><a href="#定义客户端handler" class="headerlink" title="定义客户端handler"></a>定义客户端handler</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyClientHandler</span> <span class="keyword">extends</span> <span class="title">SimpleChannelInboundHandler</span>&lt;<span class="title">String</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">channelRead0</span><span class="params">(ChannelHandlerContext ctx, String msg)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        System.out.println(ctx.channel().remoteAddress());</span><br><span class="line">        System.out.println(<span class="string">"client ouput: "</span> + msg);</span><br><span class="line">        ctx.channel().writeAndFlush(<span class="string">"from client:"</span>+ LocalDateTime.now());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">channelActive</span><span class="params">(ChannelHandlerContext ctx)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        ctx.writeAndFlush(<span class="string">"来自于客户端的问候!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">exceptionCaught</span><span class="params">(ChannelHandlerContext ctx, Throwable cause)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        cause.printStackTrace();</span><br><span class="line">        ctx.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/03/22/shMap和Hashtable的比较/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/22/shMap和Hashtable的比较/" class="post-title-link" itemprop="url">HashMap和Hashtable的比较</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-03-22 16:39:00 / 修改时间：16:47:57" itemprop="dateCreated datePublished" datetime="2020-03-22T16:39:00+08:00">2020-03-22</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>296</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>1 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="HashMap和Hashtable的比较"><a href="#HashMap和Hashtable的比较" class="headerlink" title="HashMap和Hashtable的比较"></a>HashMap和Hashtable的比较</h1><h2 id="共同点"><a href="#共同点" class="headerlink" title="共同点"></a>共同点</h2><h3 id="结构"><a href="#结构" class="headerlink" title="结构"></a>结构</h3><p>都是数组 + 链表的形式</p>
<h3 id="存值方式"><a href="#存值方式" class="headerlink" title="存值方式"></a>存值方式</h3><p>都是根据key的hash计算，再将(key,value)组装为Entry或Node</p>
<h2 id="区别"><a href="#区别" class="headerlink" title="区别"></a>区别</h2><h3 id="put区别"><a href="#put区别" class="headerlink" title="put区别"></a>put区别</h3><p>1、HashMap允许key和value都为null，而Hashtable不允许</p>
<p>2、HashMap是线程不安全的，Hashtable是线程安全的。</p>
<p>3、HashMap存值，在链表长度 &gt; 8 并且 数组长度 &lt; 64，会进行扩容，如果数组长度 &gt; 64，则将链表转换为红黑树。</p>
<p>Hashtable存值只有在table的size达到临界值时才会进行扩容</p>
<p>4、HashMap在链表存值时尾部插入，而Hashtable是头部插入</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/03/21/ashMap原理分析-一/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/21/ashMap原理分析-一/" class="post-title-link" itemprop="url">HashMap原理分析(一)</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-21 16:26:00" itemprop="dateCreated datePublished" datetime="2020-03-21T16:26:00+08:00">2020-03-21</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-03-22 16:48:12" itemprop="dateModified" datetime="2020-03-22T16:48:12+08:00">2020-03-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.5k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="HashMap的底层原理-一"><a href="#HashMap的底层原理-一" class="headerlink" title="HashMap的底层原理(一)"></a>HashMap的底层原理(一)</h1><h2 id="组成结构"><a href="#组成结构" class="headerlink" title="组成结构"></a>组成结构</h2><p>数据 + 链表的形式+红黑树</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// node 数组</span></span><br><span class="line"><span class="keyword">transient</span> Node&lt;K,V&gt;[] table;</span><br><span class="line"><span class="comment">// Node结构</span></span><br><span class="line"><span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Node</span>&lt;<span class="title">K</span>,<span class="title">V</span>&gt; <span class="keyword">implements</span> <span class="title">Map</span>.<span class="title">Entry</span>&lt;<span class="title">K</span>,<span class="title">V</span>&gt; </span>&#123;</span><br><span class="line">        <span class="keyword">final</span> <span class="keyword">int</span> hash;</span><br><span class="line">        <span class="keyword">final</span> K key;</span><br><span class="line">        V value;</span><br><span class="line">        Node&lt;K,V&gt; next;</span><br><span class="line"></span><br><span class="line">        Node(<span class="keyword">int</span> hash, K key, V value, Node&lt;K,V&gt; next) &#123;</span><br><span class="line">            <span class="keyword">this</span>.hash = hash;</span><br><span class="line">            <span class="keyword">this</span>.key = key;</span><br><span class="line">            <span class="keyword">this</span>.value = value;</span><br><span class="line">            <span class="keyword">this</span>.next = next;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="其他参数"><a href="#其他参数" class="headerlink" title="其他参数"></a>其他参数</h2><h3 id="负载因子"><a href="#负载因子" class="headerlink" title="负载因子"></a>负载因子</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * The load factor used when none specified in constructor.</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">float</span> DEFAULT_LOAD_FACTOR = <span class="number">0.75f</span>;</span><br></pre></td></tr></table></figure>
<h3 id="链表最大长度："><a href="#链表最大长度：" class="headerlink" title="链表最大长度："></a>链表最大长度：</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TREEIFY_THRESHOLD = <span class="number">8</span>;</span><br></pre></td></tr></table></figure>
<h3 id="链表转换为红黑树的重要参数"><a href="#链表转换为红黑树的重要参数" class="headerlink" title="链表转换为红黑树的重要参数"></a>链表转换为<em>红黑树</em>的重要参数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 链表长度 &gt; 8 并且数组长度小于64那么进行扩容，否则转为红黑树</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MIN_TREEIFY_CAPACITY = <span class="number">64</span>;</span><br></pre></td></tr></table></figure>
<h3 id="链表长度的界限"><a href="#链表长度的界限" class="headerlink" title="链表长度的界限"></a>链表长度的界限</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TREEIFY_THRESHOLD = <span class="number">8</span>;</span><br></pre></td></tr></table></figure>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义HashMap，初始大小可不写</span></span><br><span class="line">HashMap&lt;String, String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;(<span class="number">5</span>);</span><br><span class="line"><span class="comment">// 存放值</span></span><br><span class="line">map.put(<span class="string">"test"</span>,<span class="string">"name2"</span>);</span><br><span class="line"><span class="comment">// 取值</span></span><br><span class="line">String test = map.get(<span class="string">"test"</span>);</span><br></pre></td></tr></table></figure>
<h2 id="原理解析"><a href="#原理解析" class="headerlink" title="原理解析"></a>原理解析</h2><h3 id="简括流程"><a href="#简括流程" class="headerlink" title="简括流程"></a>简括流程</h3><ul>
<li><p>计算key的<code>hash</code>,高16位于低16位进行异或运算</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> (key == <span class="keyword">null</span>) ? <span class="number">0</span> : (h = key.hashCode()) ^ (h &gt;&gt;&gt; <span class="number">16</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>判断数组是否为空，为空时初始化。采用初次扩容。初始化的数组大小为<em>16</em>，扩容界限为<em>16x0.75=12</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; p; <span class="keyword">int</span> n, i;</span><br><span class="line"><span class="keyword">if</span> ((tab = table) == <span class="keyword">null</span> || (n = tab.length) == <span class="number">0</span>)</span><br><span class="line">    n = (tab = resize()).length;</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用hash&amp;(n-1)获取key存放的下标位置，初始化<code>Node</code>，存放进<code>Node [i]</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> ((p = tab[i = (n - <span class="number">1</span>) &amp; hash]) == <span class="keyword">null</span>)</span><br><span class="line">    tab[i] = newNode(hash, key, value, <span class="keyword">null</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>key的hash值与初始化的第一个<code>Node</code>的hash一致，并且key的值也一致，直接覆盖</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Node&lt;K,V&gt; e; K k;</span><br><span class="line"><span class="keyword">if</span> (p.hash == hash &amp;&amp;</span><br><span class="line">    ((k = p.key) == key || (key != <span class="keyword">null</span> &amp;&amp; key.equals(k))))</span><br><span class="line">    e = p;</span><br></pre></td></tr></table></figure>
</li>
<li><p>如果是红黑树，直接存放进红黑树内</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">e = ((TreeNode&lt;K,V&gt;)p).putTreeVal(<span class="keyword">this</span>, tab, hash, key, value);</span><br></pre></td></tr></table></figure>
</li>
<li><p>遍历<code>Node</code>链表，直到<code>next == null</code>，创建新的<code>Node</code>节点赋值给<code>next</code>。如果链表大小大于8个，则进行扩容或转红黑树操作</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> binCount = <span class="number">0</span>; ; ++binCount) &#123;</span><br><span class="line">    <span class="keyword">if</span> ((e = p.next) == <span class="keyword">null</span>) &#123;</span><br><span class="line">        p.next = newNode(hash, key, value, <span class="keyword">null</span>);</span><br><span class="line">        <span class="keyword">if</span> (binCount &gt;= TREEIFY_THRESHOLD - <span class="number">1</span>) <span class="comment">// -1 for 1st</span></span><br><span class="line">            treeifyBin(tab, hash);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (e.hash == hash &amp;&amp;</span><br><span class="line">        ((k = e.key) == key || (key != <span class="keyword">null</span> &amp;&amp; key.equals(k))))</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    p = e;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>判断map大小是否大于扩容大小，是的话进行扩容</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// size:hashMao大小</span></span><br><span class="line"><span class="comment">// threshold：threshold = newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);</span></span><br><span class="line"><span class="keyword">if</span> (++size &gt; threshold)</span><br><span class="line">    resize();</span><br></pre></td></tr></table></figure>
</li>
<li><p>覆盖旧值操作</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (e != <span class="keyword">null</span>) &#123; <span class="comment">// existing mapping for key</span></span><br><span class="line">    V oldValue = e.value;</span><br><span class="line">    <span class="keyword">if</span> (!onlyIfAbsent || oldValue == <span class="keyword">null</span>)</span><br><span class="line">        e.value = value;</span><br><span class="line">    afterNodeAccess(e);</span><br><span class="line">    <span class="keyword">return</span> oldValue;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="详细代码"><a href="#详细代码" class="headerlink" title="详细代码"></a>详细代码</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 扩容操作 </span></span><br><span class="line"><span class="keyword">final</span> Node&lt;K,V&gt;[] resize() &#123;</span><br><span class="line">     <span class="comment">// 数组</span></span><br><span class="line">    Node&lt;K,V&gt;[] oldTab = table;</span><br><span class="line">    <span class="comment">// 旧数组的大小</span></span><br><span class="line">    <span class="keyword">int</span> oldCap = (oldTab == <span class="keyword">null</span>) ? <span class="number">0</span> : oldTab.length;</span><br><span class="line">    <span class="comment">// 扩容界限</span></span><br><span class="line">    <span class="keyword">int</span> oldThr = threshold;</span><br><span class="line">    <span class="comment">// 扩容后的大小，扩容后的界限</span></span><br><span class="line">    <span class="keyword">int</span> newCap, newThr = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (oldCap &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (oldCap &gt;= MAXIMUM_CAPACITY) &#123;</span><br><span class="line">            threshold = Integer.MAX_VALUE;</span><br><span class="line">            <span class="keyword">return</span> oldTab;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 成倍的扩容,16 = 010000 &lt;&lt; 1 --&gt; 100000 = 32</span></span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> ((newCap = oldCap &lt;&lt; <span class="number">1</span>) &lt; MAXIMUM_CAPACITY &amp;&amp;</span><br><span class="line">                 oldCap &gt;= DEFAULT_INITIAL_CAPACITY)</span><br><span class="line">            newThr = oldThr &lt;&lt; <span class="number">1</span>; <span class="comment">// double threshold</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (oldThr &gt; <span class="number">0</span>) <span class="comment">// initial capacity was placed in threshold</span></span><br><span class="line">        newCap = oldThr;</span><br><span class="line">    <span class="keyword">else</span> &#123;               <span class="comment">// zero initial threshold signifies using defaults</span></span><br><span class="line">        <span class="comment">// 初始化时的扩容操作</span></span><br><span class="line">        newCap = DEFAULT_INITIAL_CAPACITY;</span><br><span class="line">        newThr = (<span class="keyword">int</span>)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (newThr == <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">float</span> ft = (<span class="keyword">float</span>)newCap * loadFactor;</span><br><span class="line">        newThr = (newCap &lt; MAXIMUM_CAPACITY &amp;&amp; ft &lt; (<span class="keyword">float</span>)MAXIMUM_CAPACITY ?</span><br><span class="line">                  (<span class="keyword">int</span>)ft : Integer.MAX_VALUE);</span><br><span class="line">    &#125;</span><br><span class="line">    threshold = newThr;</span><br><span class="line">    <span class="meta">@SuppressWarnings</span>(&#123;<span class="string">"rawtypes"</span>,<span class="string">"unchecked"</span>&#125;)</span><br><span class="line">    Node&lt;K,V&gt;[] newTab = (Node&lt;K,V&gt;[])<span class="keyword">new</span> Node[newCap];</span><br><span class="line">    table = newTab;</span><br><span class="line">    <span class="comment">// 扩容后的数据迁移</span></span><br><span class="line">    <span class="keyword">if</span> (oldTab != <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; oldCap; ++j) &#123;</span><br><span class="line">            Node&lt;K,V&gt; e;</span><br><span class="line">            <span class="keyword">if</span> ((e = oldTab[j]) != <span class="keyword">null</span>) &#123;</span><br><span class="line">                oldTab[j] = <span class="keyword">null</span>;</span><br><span class="line">                <span class="keyword">if</span> (e.next == <span class="keyword">null</span>)</span><br><span class="line">                    newTab[e.hash &amp; (newCap - <span class="number">1</span>)] = e;</span><br><span class="line">                <span class="keyword">else</span> <span class="keyword">if</span> (e <span class="keyword">instanceof</span> TreeNode)</span><br><span class="line">                    ((TreeNode&lt;K,V&gt;)e).split(<span class="keyword">this</span>, newTab, j, oldCap);</span><br><span class="line">                <span class="keyword">else</span> &#123; <span class="comment">// preserve order</span></span><br><span class="line">                    <span class="comment">// 扩容数据的低位链表,低位不变</span></span><br><span class="line">                    Node&lt;K,V&gt; loHead = <span class="keyword">null</span>, loTail = <span class="keyword">null</span>;</span><br><span class="line">                    <span class="comment">// 扩容数据的高位链表，高位迁移</span></span><br><span class="line">                    Node&lt;K,V&gt; hiHead = <span class="keyword">null</span>, hiTail = <span class="keyword">null</span>;</span><br><span class="line">                    Node&lt;K,V&gt; next;</span><br><span class="line">                    <span class="keyword">do</span> &#123;</span><br><span class="line">                        next = e.next;</span><br><span class="line">                        <span class="comment">/**</span></span><br><span class="line"><span class="comment">                        * oldCap 16: 10000</span></span><br><span class="line"><span class="comment">                        * 假设e.hash = 9</span></span><br><span class="line"><span class="comment">                        * 第一次存放的位置:01001 &amp; 01111 = 01001 = 9</span></span><br><span class="line"><span class="comment">                        * 为了保证扩容后用同样的操作取下标结果不会改变，也就是通过(n - 1) &amp; hash还会取到e</span></span><br><span class="line"><span class="comment">                        * 扩容后：newCap = 32 = 100000</span></span><br><span class="line"><span class="comment">                        * 001001 &amp; 011111 = 01001 = 9</span></span><br><span class="line"><span class="comment">                        * 所以对e这个数据不改变下标位置</span></span><br><span class="line"><span class="comment">                        * 01001 &amp;</span></span><br><span class="line"><span class="comment">                        * 10000 = 0</span></span><br><span class="line"><span class="comment">                        **/</span></span><br><span class="line">                        <span class="keyword">if</span> ((e.hash &amp; oldCap) == <span class="number">0</span>) &#123;</span><br><span class="line">                            <span class="keyword">if</span> (loTail == <span class="keyword">null</span>)</span><br><span class="line">                                loHead = e;</span><br><span class="line">                            <span class="keyword">else</span></span><br><span class="line">                                loTail.next = e;</span><br><span class="line">                            loTail = e;</span><br><span class="line">                        &#125;</span><br><span class="line">                         <span class="comment">/**</span></span><br><span class="line"><span class="comment">                        * oldCap 16: 10000</span></span><br><span class="line"><span class="comment">                        * 假设e.hash = 20</span></span><br><span class="line"><span class="comment">                        * 第一次存放的位置:10100 &amp; 01111 = 00100 = 4</span></span><br><span class="line"><span class="comment">                        * 为了保证扩容后用同样的操作取下标结果不会改变，也就是通过(n - 1) &amp; hash还会取到e</span></span><br><span class="line"><span class="comment">                        * 扩容后：newCap = 32 = 100000</span></span><br><span class="line"><span class="comment">                        * 010100 &amp; 011111 = 010100 = 20</span></span><br><span class="line"><span class="comment">                        * 所以对e这个数据要改变下标位置为20</span></span><br><span class="line"><span class="comment">                        * 10100 &amp;</span></span><br><span class="line"><span class="comment">                        * 10000 = 10000 != 0</span></span><br><span class="line"><span class="comment">                        * 所以要对该数据进行迁移，迁移坐标为：4 + 16(扩容的大小) = 20</span></span><br><span class="line"><span class="comment">                        **/</span></span><br><span class="line">                        <span class="keyword">else</span> &#123;</span><br><span class="line">                            <span class="keyword">if</span> (hiTail == <span class="keyword">null</span>)</span><br><span class="line">                                hiHead = e;</span><br><span class="line">                            <span class="keyword">else</span></span><br><span class="line">                                hiTail.next = e;</span><br><span class="line">                            hiTail = e;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125; <span class="keyword">while</span> ((e = next) != <span class="keyword">null</span>);</span><br><span class="line">                    <span class="keyword">if</span> (loTail != <span class="keyword">null</span>) &#123;</span><br><span class="line">                        loTail.next = <span class="keyword">null</span>;</span><br><span class="line">                        newTab[j] = loHead;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">if</span> (hiTail != <span class="keyword">null</span>) &#123;</span><br><span class="line">                        hiTail.next = <span class="keyword">null</span>;</span><br><span class="line">                        newTab[j + oldCap] = hiHead;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newTab;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/03/21/条http请求的处理过程/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/21/条http请求的处理过程/" class="post-title-link" itemprop="url">一条http请求的处理过程</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-03-21 15:09:00 / 修改时间：15:21:16" itemprop="dateCreated datePublished" datetime="2020-03-21T15:09:00+08:00">2020-03-21</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/网络通讯/" itemprop="url" rel="index"><span itemprop="name">网络通讯</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>234</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>1 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="一条http请求的处理过程"><a href="#一条http请求的处理过程" class="headerlink" title="一条http请求的处理过程"></a>一条http请求的处理过程</h1><p><img src="../images/http请求.png" alt="http请求"></p>
<h2 id="client发送请求"><a href="#client发送请求" class="headerlink" title="client发送请求"></a>client发送请求</h2><h2 id="DNS解析请求得域名-—-gt-ip地址"><a href="#DNS解析请求得域名-—-gt-ip地址" class="headerlink" title="DNS解析请求得域名 —&gt; ip地址"></a>DNS解析请求得域名 —&gt; ip地址</h2><h2 id="TCP-IP-UDP-IP四层传输协议"><a href="#TCP-IP-UDP-IP四层传输协议" class="headerlink" title="TCP/IP,UDP/IP四层传输协议"></a>TCP/IP,UDP/IP四层传输协议</h2><h3 id="应用层"><a href="#应用层" class="headerlink" title="应用层"></a>应用层</h3><p>发送http请求</p>
<h3 id="传输层"><a href="#传输层" class="headerlink" title="传输层"></a>传输层</h3><p>组装TCP头信息 —&gt; 【TCP头信息】【http请求报文】</p>
<h3 id="网络层"><a href="#网络层" class="headerlink" title="网络层"></a>网络层</h3><p>组装IP信息 —&gt; 【ip信息】【TCP头信息】【http请求报文】</p>
<h3 id="网络接口层"><a href="#网络接口层" class="headerlink" title="网络接口层"></a>网络接口层</h3><p>组装MAC信息 —&gt; 【MAC信息】【ip信息】【TCP头信息】【http请求报文】</p>
<h2 id="网络接口层发送数据"><a href="#网络接口层发送数据" class="headerlink" title="网络接口层发送数据"></a>网络接口层发送数据</h2><p>发送这样的数据：[01010110101010]</p>
<h2 id="目标服务器处理数据"><a href="#目标服务器处理数据" class="headerlink" title="目标服务器处理数据"></a>目标服务器处理数据</h2>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/03/12/dis的使用及集群/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/12/dis的使用及集群/" class="post-title-link" itemprop="url">redis的使用及集群</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-12 14:30:00" itemprop="dateCreated datePublished" datetime="2020-03-12T14:30:00+08:00">2020-03-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-03-22 16:48:33" itemprop="dateModified" datetime="2020-03-22T16:48:33+08:00">2020-03-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/redis/" itemprop="url" rel="index"><span itemprop="name">redis</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>1.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>2 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="redis数据结构-："><a href="#redis数据结构-：" class="headerlink" title="redis数据结构 ："></a>redis数据结构 ：</h2><h3 id="string"><a href="#string" class="headerlink" title="string:"></a>string:</h3><p><code>set key,value ,set key value ex time</code></p>
<h3 id="map"><a href="#map" class="headerlink" title="map"></a>map</h3><p>所有的键值对的键和值的长度都小于等于64byte，哈希对象保存的键值对数量都小于512：ziplist，hashtable 对象 </p>
<h3 id="list"><a href="#list" class="headerlink" title="list"></a>list</h3><p>quicklist 消息队列</p>
<h3 id="set"><a href="#set" class="headerlink" title="set"></a>set</h3><p>inset：如果元素类型都是整型，并且元素个数不小于512，hashtab，点赞，互相关注</p>
<h3 id="zset"><a href="#zset" class="headerlink" title="zset"></a>zset</h3><p>元素数量小于128，所有member的长度小于64字节使用ziplist，否则升级为skiplist，热点排序 ，有一个score</p>
<h2 id="redis常用命令"><a href="#redis常用命令" class="headerlink" title="redis常用命令"></a>redis常用命令</h2><h3 id="list-1"><a href="#list-1" class="headerlink" title="list"></a>list</h3><p>lpush：左插入数据</p>
<p>lpushx：做插入数据， 前提条件是key存在并且key为列表</p>
<p>rpush：右插入</p>
<p>rpushx：右插入， 前提条件是key存在并且key为列表</p>
<p>rpop：右弹出</p>
<p>lpop：左弹出</p>
<p>lrange：弹出一个范围内的数据</p>
<p>blpop：左阻塞式的弹出</p>
<p>brpop：右阻塞式的弹出</p>
<h3 id="set-1"><a href="#set-1" class="headerlink" title="set"></a>set</h3><p>1、set：sadd添加，spop删除，smembers 查看所有元素，sinter交集，sunion合集，sdiff差集</p>
<h3 id="map-1"><a href="#map-1" class="headerlink" title="map"></a>map</h3><h3 id="zset-1"><a href="#zset-1" class="headerlink" title="zset"></a>zset</h3><h3 id="string-1"><a href="#string-1" class="headerlink" title="string"></a>string</h3><h2 id="redis热点数据："><a href="#redis热点数据：" class="headerlink" title="redis热点数据："></a>redis热点数据：</h2><p>1、客户端统计的方式</p>
<p>2、代理层：TwemProxy或 Codis</p>
<p>3、服务端统计：monitor监控，但是只能监控到一个节点</p>
<p>4、机器层面：抓包的形式</p>
<h2 id="redis查看内存使用信息："><a href="#redis查看内存使用信息：" class="headerlink" title="redis查看内存使用信息："></a>redis查看内存使用信息：</h2><p><code>info memory</code></p>
<h2 id="Redis主从复制"><a href="#Redis主从复制" class="headerlink" title="Redis主从复制"></a>Redis主从复制</h2><p>master node 第一次执行全量复制，通过bgsave命令在本地生成一份RDB文件，将RDB快照文件发送给slave节点，slave node 会先清空自己的旧数据，然后从RDB文件中加载数据。</p>
<h3 id="在生成rdb文件中，master-node-如果接收到新的写命令怎么办？"><a href="#在生成rdb文件中，master-node-如果接收到新的写命令怎么办？" class="headerlink" title="在生成rdb文件中，master node 如果接收到新的写命令怎么办？"></a>在生成rdb文件中，master node 如果接收到新的写命令怎么办？</h3><p>master会把所有的新的写命令缓存在内存中，在slave node 保存了RDB文件之后，再讲新的命令复制给slave node</p>
<h3 id="主从复制的不足："><a href="#主从复制的不足：" class="headerlink" title="主从复制的不足："></a>主从复制的不足：</h3><p>1、当RDB文件过大时同步非常耗时</p>
<p>2、在一主多从情况下，当master node 挂了后。对外服务就不可用了，单点问题并没有解决。如果每次都手动把之前的服务器切换为主服务器，这就比较费力。</p>
<h2 id="可用性保证之sentinel"><a href="#可用性保证之sentinel" class="headerlink" title="可用性保证之sentinel"></a>可用性保证之sentinel</h2><p>sentinel是一个监听，监听redis集群中的master和slave，如果master一定时间内没有给sentinel回复消息，则将master标记为下线，然后把某一个slave标记为master，应用每一次都从这个监控服务器拿到master的地址。</p>
<p>sentinel通过info命令来获取到redis集群中的节点的信息。</p>
<p>服务下线：当sentinel监听到master节点下线后，会进行故障转移</p>
<p>故障转移：故障转移的第一步就是在sentinel集群中选举出一个leader节点，然后让这个leader指定出一个master节点。</p>
<h3 id="sentinel集群选举算法"><a href="#sentinel集群选举算法" class="headerlink" title="sentinel集群选举算法"></a>sentinel集群选举算法</h3><p>类似raft算法。raft算法：首先会生成一个150-300ms的时间，默认最小时间的为leader，如果时间一样则再次生成时间。</p>
<p>1、master客观下线会触发选举，而不是过了时间才会</p>
<p>2、leader并不会把自己成为leader的信息发送给其他sentinel节点。其他sentinel等待leader从slave选举出master后，检测新的master节点正常运行后，就会去掉客观下线的标识，从而不需要进入故障转移流程。</p>
<h3 id="sentienl根据什么来指定master节点："><a href="#sentienl根据什么来指定master节点：" class="headerlink" title="sentienl根据什么来指定master节点："></a>sentienl根据什么来指定master节点：</h3><p>1、断开连接时间：如果与哨兵连接断开时间过长，超过了某个阈值，就直接失去了选举权，</p>
<p>2、优先级：如果拥有了选举权，那就判断谁的优先级更高，数值越小优先级越高</p>
<p>3、复制数量：如果优先级也一样，就判断谁从master节点中复制的数据最多。</p>
<p>4、进程id：如果复制数量一样，就选择进程id最小的那个。</p>
<h3 id="哨兵机制的不足："><a href="#哨兵机制的不足：" class="headerlink" title="哨兵机制的不足："></a>哨兵机制的不足：</h3><p>主从切换中会丢失数据，因为只有一个master，salve节点 的数据使用RDB文件复制的。</p>
<p>只能单点写，没有解决扩容的问题</p>
<p>如果数据量过大，我们就需要多个master-slave的group</p>
<h2 id="redis数据的分片"><a href="#redis数据的分片" class="headerlink" title="redis数据的分片"></a>redis数据的分片</h2><h3 id="客户端实现"><a href="#客户端实现" class="headerlink" title="客户端实现"></a>客户端实现</h3><p>客户端根据key进行hash计算，来进行分片。</p>
<p>ShardedJedis</p>
<h3 id="代理层"><a href="#代理层" class="headerlink" title="代理层"></a>代理层</h3><p>将分片逻辑提取出来，运行一个独立的代理服务，客户端连接到这个代理服务，代理服务做请求转发。</p>
<p>Twemproxy，Codis</p>
<h3 id="服务层"><a href="#服务层" class="headerlink" title="服务层"></a>服务层</h3><p>Redis Cluster</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/03/04/va面试题整理/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/04/va面试题整理/" class="post-title-link" itemprop="url">Java面试题整理</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-04 19:36:00" itemprop="dateCreated datePublished" datetime="2020-03-04T19:36:00+08:00">2020-03-04</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-03-22 16:48:47" itemprop="dateModified" datetime="2020-03-22T16:48:47+08:00">2020-03-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3.4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="JAVA"><a href="#JAVA" class="headerlink" title="JAVA"></a>JAVA</h2><ol>
<li>多个线程同时读写，读线程的数量量远远⼤大于写线程，你认为应该如何解决 并发的问题？你会选择加什么样的锁？</li>
<li>JAVA的AQS是否了了解，它是⼲干嘛的？</li>
<li>除了了synchronized关键字之外，你是怎么来保障线程安全的？</li>
<li>什么时候需要加volatile关键字？它能保证线程安全吗？</li>
<li>线程池内的线程如果全部忙，提交⼀一个新的任务，会发⽣生什么？队列列全部 塞满了了之后，还是忙，再提交会发⽣生什什么？</li>
<li>Tomcat本身的参数你⼀一般会怎么调整？</li>
<li>synchronized关键字锁住的是什么东⻄西？在字节码中是怎么表示的？在内 存中的对象上表现为什么？</li>
<li>wait/notify/notifyAll⽅方法需不不需要被包含在synchronized块中？这是为什 么？</li>
<li>ExecutorService你⼀一般是怎么⽤用的？是每个service放⼀一个还是⼀一个项⽬目<br>⾥里里⾯面放⼀一个？有什么好处？<h2 id="Spring"><a href="#Spring" class="headerlink" title="Spring"></a>Spring</h2></li>
<li>你有没有⽤用过Spring的AOP? 是⽤用来干嘛的? ⼤大概会怎么使⽤用？</li>
<li>如果一个接口有2个不同的实现, 那么怎么来Autowire⼀一个指定的实现？</li>
<li>Spring的声明式事务 @Transaction注解⼀一般写在什么位置? 抛出了了异常会⾃自动回滚吗？有没有办法控制不不触发回滚?</li>
<li>如果想在某个Bean⽣生成并装配完毕后执⾏行行⾃自⼰己的逻辑，可以什么⽅方式实现？</li>
<li>SpringBoot没有放到web容器器⾥里里为什么能跑HTTP服务？</li>
<li>SpringBoot中如果你想使⽤用⾃自定义的配置⽂文件⽽而不不仅仅是<br>application.properties，应该怎么弄弄？</li>
<li>SpringMVC中RequestMapping可以指定GET, POST方法么？怎么指定？</li>
<li>SpringMVC如果希望把输出的Object(例例如XXResult或者XXResponse)这种包装为JSON输出, 应该怎么处理理?</li>
<li>怎样拦截SpringMVC的异常，然后做⾃自定义的处理理，比如打⽇日志或者包装成JSON<h2 id="MySQL"><a href="#MySQL" class="headerlink" title="MySQL"></a>MySQL</h2></li>
<li>如果有很多数据插⼊入MYSQL 你会选择什么⽅方式?</li>
<li>如果查询很慢，你会想到的第⼀一个⽅方式是什么？索引是⼲干嘛的?</li>
<li>如果建了了⼀一个单列列索引，查询的时候查出2列列，会⽤用到这个单列列索引吗？</li>
<li>如果建了了⼀一个包含多个列列的索引，查询的时候只⽤用了了第⼀一列列，能不不能⽤用上这个索引？查三列列呢？</li>
<li>接上题，如果where条件后⾯面带有⼀一个 i + 5 &lt; 100 会使⽤用到这个索引吗？</li>
<li>怎么看是否⽤用到了了某个索引？</li>
<li>like %aaa%会使⽤用索引吗? like aaa%呢?</li>
<li>drop、truncate、delete的区别？</li>
<li>平时你们是怎么监控数据库的? 慢SQL是怎么排查的？</li>
<li>你们数据库是否⽀支持emoji表情，如果不不⽀支持，如何操作?</li>
<li>你们的数据库单表数据量量是多少？⼀一般多⼤大的时候开始出现查询性能急剧下降？</li>
<li>查询死掉了了，想要找出执⾏行行的查询进程⽤用什什么命令？找出来之后⼀一般你 会⼲干嘛？</li>
<li>读写分离是怎么做的？你认为中间件会怎么来操作？这样操作跟事务有 什什么关系？</li>
<li>分库分表有没有做过？线上的迁移过程是怎么样的？如何确定数据是正确的？<h2 id="JVM"><a href="#JVM" class="headerlink" title="JVM"></a>JVM</h2></li>
<li>你知道哪些或者你们线上使⽤用什什么GC策略略? 它有什什么优势，适⽤用于什什么场景？</li>
<li>JAVA类加载器器包括⼏几种？它们之间的⽗父⼦子关系是怎么样的？双亲委派机制是什什么意思？有什什么好处？</li>
<li>如何⾃自定义⼀一个类加载器器？你使⽤用过哪些或者你在什什么场景下需要⼀一个⾃自定义的类加载器器吗？</li>
<li>堆内存设置的参数是什什么？</li>
<li>Perm Space中保存什什么数据? 会引起OutOfMemory吗？</li>
<li>做gc时，⼀一个对象在内存各个Space中被移动的顺序是什什么？</li>
<li>你有没有遇到过OutOfMemory问题？你是怎么来处理理这个问题的？处理理过程中有哪些收获？</li>
<li>1.8之后Perm Space有哪些变动? MetaSpace⼤大⼩小默认是⽆无限的么? 还是你们会通过什什么⽅方式来指定⼤大⼩小?</li>
<li>Jstack是⼲干什什么的? Jstat呢? 如果线上程序周期性地出现卡顿，你怀疑可能是gc导致的，你会怎么来排查这个问题？线程⽇日志⼀一般你会看其中的什什么   部分？</li>
<li>StackOverFlow异常有没有遇到过？⼀一般你猜测会在什什么情况下被触发？如何指定⼀一个线程的堆栈⼤大⼩小？⼀一般你们写多少？<h2 id="Linux命令"><a href="#Linux命令" class="headerlink" title="Linux命令"></a>Linux命令</h2></li>
<li>⽇日志特别⼤大只想看最后100⾏行行怎么弄弄?   如果想⼀一直看⽇日志的持续输出，⽤用什什么命令?</li>
<li>如果⽇日志⼀一边输出，⼀一边想实时看到有没有某个关键字应该怎么弄弄？</li>
<li>grep如果忽略略⼤大⼩小写应该怎么弄弄? 正则表达式呢？</li>
<li>vim往下⼀一⾏行行是什什么键？往下30⾏行行呢? 跳到⽂文件末尾⼀一⾏行行是什什么? 跳回来是什什么? 向后搜索是什什么?</li>
<li>如果有个⽂文本⽂文件，按空格作为列列的分隔符，如果想统计第三列列⾥里里⾯面的每个单词的出现次数应该怎么弄弄？</li>
<li>如果把上⾯面的出现次数排个序应该怎么弄弄?  想按照数字本身的顺序⽽而不不是字符串串的顺序排列列怎么弄弄？</li>
<li>Linux环境变量量是以什什么作为分隔符的？环境变量量通过什什么命令设置？</li>
<li>给某个⽂文件权设置限⽐比如设置为644 是⽤用什什么命令？这个6是什什么意思？</li>
<li>Linux下⾯面如果想看某个进程的资源占⽤用情况是怎么看的？系统load⼤大概 指的什什么意思？你们线上系统load⼀一般多少？如果⼀一个4核机器器，你认为多少load是⽐比较正常的？top命令⾥里里⾯面按⼀一下1会发⽣生什什么?</li>
<li>top命令⾥里里⾯面，有时候所有进程的CPU使⽤用率加起来超过100%是怎么回  事？</li>
<li>还有哪些查看系统性能或者供你发现问题的命令？你⼀一般是看哪个参数？</li>
<li>想看某个进程打开了了哪些⽹网络连接是什什么命令？⾥里里⾯面连接的状态你⽐比较关⼼心哪⼏几种？<br>– 偏题</li>
<li>有没有做过Linux系统参数⽅方⾯面的优化，⼤大概优化过什什么？</li>
<li>系统参数⾥里里⾯面有个叫做backlog的可以⽤用来⼲干什什么？</li>
<li>查看⽹网络连接发现好多TIME_WAIT 可能是什什么原因？对你的应⽤用会有什什么影响？你会选择什什么样的⽅方式来减少这些TIME_WAIT</li>
<li>可否介绍⼀一下TCP三次握⼿手的过程，如果现在有个⽹网络程序，你⽤用第三⽅方  的library来发送数据，你怀疑这个library发送的数据有问题，那么怎么来验   证？tcpdump导出的⽂文件你⼀一般是怎么分析的？</li>
<li>KeepAlive是⽤用来⼲干什什么的？这样的好处是什什么？<h2 id="Redis"><a href="#Redis" class="headerlink" title="Redis"></a>Redis</h2>– 开发</li>
<li>缓存穿透可以介绍⼀一下么？你认为应该如何解决这个问题</li>
<li>你是怎么触发缓存更更新的？(⽐比如设置超时时间(被动⽅方式),    ⽐比如更更新的时候主动update)？如果是被动的⽅方式如何控制多个⼊入⼝口同时触发某个缓存更更新？</li>
<li>你们⽤用Redis来做什什么？为什什么不不⽤用其他的KV存储例例如Memcached,<br>Cassandra等?</li>
<li>你们⽤用什什么Redis客户端? Redis⾼高性能的原因⼤大概可以讲⼀一些?</li>
<li>你熟悉哪些Redis的数据结构? zset是⼲干什什么的? 和set有什什么区别?</li>
<li>Redis的hash, 存储和获取的具体命令叫什什么名字?</li>
<li>LPOP和BLPOP的区别?</li>
<li>Redis的有⼀一些包含SCAN关键字的命令是⼲干嘛的?  SCAN返回的数据量量是固定的吗?</li>
<li>Redis中的Lua有没有使⽤用过? 可以⽤用来做什什么? 为什什么可以这么⽤用?</li>
<li>Redis的Pipeline是⽤用来⼲干什什么的?<br>– 运维</li>
<li>Redis持久化⼤大概有⼏几种⽅方式? aof和rdb的区别是什什么?  AOF有什什么优缺点吗?</li>
<li>Redis Replication的⼤大致流程是什什么? bgsave这个命令的执⾏行行过程?<br>– 偏题</li>
<li>如果有很多 KV数据要存储到Redis, 但是内存不不⾜足, 通过什什么⽅方式可以缩减内存? 为什什么这样可以缩⼩小内存?</li>
<li>Redis中List, HashTable都⽤用到了了ZipList, 为什什么会选择它?</li>
</ol>
<h2 id="监控、稳定性"><a href="#监控、稳定性" class="headerlink" title="监控、稳定性"></a>监控、稳定性</h2><ol>
<li>业务⽇日志是通过什什么⽅方式来收集的？</li>
<li>线上机器器如何监控？采⽤用什什么开源产品或者⾃自研的产品？它是分钟级的还 是秒级的？</li>
<li>如果让你来想办法收集⼀一个JAVA后端应⽤用的性能数据，你会在意哪些⽅方<br>⾯面? 你会选择什什么样的⼯工具、思路路来收集?</li>
<li>⼀一般你调⽤用第三⽅方的时候会不不会监控调⽤用情况？</li>
</ol>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

        
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://github.com/LBraveHeart/2020/03/04/池的基本使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liu666">
      <meta itemprop="description" content="仰面朝天，知此身渺渺，沧海一粟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
            <a href="/2020/03/04/池的基本使用/" class="post-title-link" itemprop="url">线程池的基本使用</a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-04 17:39:00" itemprop="dateCreated datePublished" datetime="2020-03-04T17:39:00+08:00">2020-03-04</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-03-22 16:49:04" itemprop="dateModified" datetime="2020-03-22T16:49:04+08:00">2020-03-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>896</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>1 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="线程池的基本使用"><a href="#线程池的基本使用" class="headerlink" title="线程池的基本使用"></a>线程池的基本使用</h2><h3 id="线程池的参数："><a href="#线程池的参数：" class="headerlink" title="线程池的参数："></a>线程池的参数：</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">ThreadPoolExecutor</span><span class="params">(<span class="keyword">int</span> corePoolSize,// 核心线程数</span></span></span><br><span class="line"><span class="function"><span class="params">                              <span class="keyword">int</span> maximumPoolSize,// 最大线程数</span></span></span><br><span class="line"><span class="function"><span class="params">                              <span class="keyword">long</span> keepAliveTime,// 超过核心线程数的其他线程存活时间</span></span></span><br><span class="line"><span class="function"><span class="params">                              TimeUnit unit,// 超过核心线程数的其他线程存活时间单位</span></span></span><br><span class="line"><span class="function"><span class="params">                              BlockingQueue&lt;Runnable&gt; workQueue,// 阻塞队列</span></span></span><br><span class="line"><span class="function"><span class="params">                              ThreadFactory threadFactory,// 线程创建工厂</span></span></span><br><span class="line"><span class="function"><span class="params">                              RejectedExecutionHandler handler)</span> <span class="comment">// 拒绝策略</span></span></span><br></pre></td></tr></table></figure>
<h3 id="阻塞队列："><a href="#阻塞队列：" class="headerlink" title="阻塞队列："></a>阻塞队列：</h3><table>
<thead>
<tr>
<th>类名</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>ArrayBlockingQueue</code></td>
<td>初始化是设置queue的大小</td>
</tr>
<tr>
<td><code>LinkedBlockingQueue</code></td>
<td>链表实现的有界阻塞队列, 此队列的默认和最大长度为<code>Integer.MAX_VALUE</code>。此队列按照先进先出的原则对元素进行排序</td>
</tr>
<tr>
<td><code>SynchronousQueue</code></td>
<td>不存储元素的阻塞队列, 每一个 put 操作必须等待一个 take 操作，否则不能继续添加元素。</td>
</tr>
<tr>
<td><code>PriorityBlockingQueue</code></td>
<td>支持优先级排序的无界阻塞队列, 默认情况下元素采取自然顺序升序排列。也可以自定义类实现 <code>compareTo()</code>方法来指定元素排序规则，或者初始化 <code>PriorityBlockingQueue</code> 时，指定构造参数 Comparator 来对元素进行排序。</td>
</tr>
<tr>
<td><code>DelayQueue</code></td>
<td>优先级队列实现的无界阻塞队列</td>
</tr>
<tr>
<td><code>LinkedTransferQueue</code></td>
<td>链表实现的无界阻塞队列</td>
</tr>
<tr>
<td><code>LinkedBlockingDeque</code></td>
<td>链表实现的双向阻塞队列</td>
</tr>
</tbody>
</table>
<h3 id="阻塞队列的操作："><a href="#阻塞队列的操作：" class="headerlink" title="阻塞队列的操作："></a>阻塞队列的操作：</h3><h5 id="增加："><a href="#增加：" class="headerlink" title="增加："></a>增加：</h5><table>
<thead>
<tr>
<th>API</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>add</td>
<td>往Queue里添加数据，如果队列满了，抛出异常</td>
</tr>
<tr>
<td>offer</td>
<td>往Queue里添加数据，添加成功return true，添加失败return false</td>
</tr>
<tr>
<td>enqueue(不能直接使用)</td>
<td>添加数据的具体操作，添加完成会signal()唤醒阻塞点线程</td>
</tr>
<tr>
<td>put</td>
<td>添加数据，阻塞式添加。</td>
</tr>
</tbody>
</table>
<h5 id="取出："><a href="#取出：" class="headerlink" title="取出："></a>取出：</h5><table>
<thead>
<tr>
<th>API</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>take</td>
<td>阻塞式取值</td>
</tr>
<tr>
<td>dequeue(不可直接使用)</td>
<td>取值核心逻辑</td>
</tr>
<tr>
<td>poll</td>
<td>非阻塞时取值</td>
</tr>
</tbody>
</table>
<h5 id="删除："><a href="#删除：" class="headerlink" title="删除："></a>删除：</h5><p>remove删除数据。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

  </div>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><span class="space">&hellip;</span><a class="page-number" href="/page/7/">7</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">liu666</p>
  <div class="site-description" itemprop="description">仰面朝天，知此身渺渺，沧海一粟</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">63</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">55</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/LBraveHeart" title="GitHub → https://github.com/LBraveHeart"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:446109730@qq.com" title="E-Mail → mailto:446109730@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liu666</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">58k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">53 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://pisces.theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  




  <script src="/js/local-search.js"></script>












  

  

</body>
</html>
